
// 每个人都有钱，我们就定义一个money
class Money implements Cloneable{
    public double m = 0.5;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    public int age;
    public Money money = new Money();
    public void eat(){
        System.out.println("睡");
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
            // super.clone() 拷贝 是 Person 类
        // 我们只需将其强转，创建一个Person 类型 的 变量 来接收它。
        Person tmp = (Person) super.clone();
//        return super.clone(); 其实以前返回 就是 tmp
        tmp.money = (Money) this.money.clone();
        return tmp;
    }
}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person) person.clone();
        person1.money.m = 2.5;
        System.out.println(person.money.m);
        System.out.println(person1.money.m);
    }



//    public static void main1(String[] args) throws CloneNotSupportedException {
//        // 通常创建对象，是通过new
//       Person person = new Person();
//       person.age = 9;
//        System.out.println(person);
//        System.out.println("==========");
//       Person person1 = (Person) person.clone();
//       person1.age = 99;
//       System.out.println(person1);
//        System.out.println(person);
//    }
}


